﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：CC
 * 公司名称：
 * 命名空间：UniversalQin
 * 唯一标识：837666df-86df-488c-9667-25be28b1e468
 * 文件名：ConfigHelper
 * 当前用户域：CC
 * 
 * 创建者：ccqin
 * 电子邮箱：ccqin2008@gmail.com
 * 创建时间：2022/10/24 14:17:52
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UniversalQin
{
    public class ConfigHelper
    {
        private static ConfigHelper instance = new ConfigHelper();
        private ConfigHelper() { }
        public static ConfigHelper Instance { get { return instance; } }


        //Configuration AppConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        //Configuration CustomConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
        //{
        //    ExeConfigFilename = System.AppDomain.CurrentDomain.BaseDirectory + @"\Configs\Custom.config"
        //}, ConfigurationUserLevel.None);
        Configuration ServerUrlConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
        {
            ExeConfigFilename = System.AppDomain.CurrentDomain.BaseDirectory + @"\Configs\ServerUrl.config"
        }, ConfigurationUserLevel.None);

#if DEBUG
        public string Url { get => GetKey(ServerUrlConfig, "Url_debug"); }
#else
        public string Url { get => GetKey(ServerUrlConfig, "Url"); }
#endif

        //public string audioPlayout { get => GetKey(CustomConfig, "AudioPlayout"); set => SetKey(CustomConfig, "AudioPlayout", value); }
        //public string isAdmin { get => GetKey(AppConfig, "IsAdmin"); set => SetKey(AppConfig, "IsAdmin", value); }




        private String GetKey(Configuration ConfigurationInstance, String key)
        {
            if (ConfigurationInstance.AppSettings.Settings[key] != null)
                return ConfigurationInstance.AppSettings.Settings[key].Value;
            else
                return string.Empty;
        }

        public static bool SetKey(Configuration ConfigurationInstance, String key, String vls)
        {
            try
            {
                if (ConfigurationInstance.AppSettings.Settings[key] != null)
                    ConfigurationInstance.AppSettings.Settings[key].Value = vls;
                else
                    ConfigurationInstance.AppSettings.Settings.Add(key, vls);
                ConfigurationInstance.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
